package com.wz.jiangsu.service.serviceImpl;


import com.wz.jiangsu.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

/**
 * @author syl
 * @description 异步执行实现类
 * @since 2021/4/23
 */
@Service
@Slf4j
public class AsyncServiceImpl implements AsyncService {


    @Override
    @Async("threadPoolTaskExecutor")
    public void executeAsync() {
        String name = Thread.currentThread().getName();
        log.info("线程{}异步执行开始",name);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("线程{}异步调用结束",name);
    }

    @Override
    @Async("threadPoolTaskExecutor")
    public Future<String> executeAsync1() {
        String name = Thread.currentThread().getName();
        log.info("线程{}异步执行开始",name);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("线程{}异步调用结束",name);
        //用AsyncResult接收线程执行结果
        return new AsyncResult<>("线程"+name+"执行结束");
    }
}
